package org.example.producerconfirm.consumer;

import com.rabbitmq.client.*;
import org.example.producerconfirm.ProducerConfirmAsync;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: anlu
 * @Date: 2021/06/22/15:10
 * @Description:
 */
public class ProducerConfirmAsyncConsumer {

    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("127.0.0.1");

        // 打开连接和创建信道
        Connection connection = factory.newConnection();
        final Channel channel = connection.createChannel();
        // 声明交换器
        channel.exchangeDeclare(ProducerConfirmAsync.EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
        // 声明一个的队列
        String queueName = ProducerConfirmAsync.QUEUE_NAME;
        channel.queueDeclare(queueName,false
        ,false,false,null);

        //只关注error级别的日志
        String severity="error";
        channel.queueBind(queueName, ProducerConfirmAsync.EXCHANGE_NAME,
                severity);

        System.out.println(" [*] Waiting for messages......");

        // 创建队列消费者
        final Consumer consumerB = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag,
                                       Envelope envelope,
                                       AMQP.BasicProperties properties,
                                       byte[] body) throws IOException {
                String message = new String(body, "UTF-8");
                //记录日志到文件：
                System.out.println( "Received ["+ envelope.getRoutingKey()
                        + "] "+message);
            }
        };
        channel.basicConsume(queueName, true, consumerB);
    }
}
